/**
 * 图片管理初始化
 */
var Image = {
    id: "imageTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Image.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '相册名称', field: 'album_name', visible: true, align: 'center', valign: 'middle'},
        {title: '图片名称', field: 'name', visible: true, align: 'center', valign: 'middle'},
        {title: '链接地址', field: 'linkurl', visible: true, align: 'center', valign: 'middle'},
        {title: '宽', field: 'width', visible: true, align: 'center', valign: 'middle'},
        {title: '高', field: 'height', visible: true, align: 'center', valign: 'middle'},
        {title: '状态', field: 'status', visible: true, align: 'center', valign: 'middle'},
        {title: '排序', field: 'sort', visible: true, align: 'center', valign: 'middle'},
        {title: '创建时间', field: 'create_time', visible: true, align: 'center', valign: 'middle'}
    ];
};

/**
 * 检查是否选中
 */
Image.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Image.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加图片
 */
Image.openAddImage = function () {
    var index = layer.open({
        type: 2,
        title: '添加图片',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/image/image_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看图片详情
 */
Image.openImageDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '图片详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/image/image_update/' + Image.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除图片
 */
Image.delete = function () {
    if (this.check()) {
        var operation = function() {
            var ajax = new $ax(Feng.ctxPath + "/image/delete", function (data) {
                Feng.success("删除成功!");
                Image.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("imageId", Image.seItem.id);
            ajax.start();
        }
        Feng.confirm("是否刪除该记录?", operation);
    }
};

/**
 * 查询图片列表
 */
Image.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    Image.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = Image.initColumn();
    var table = new BSTable(Image.id, "/image/list", defaultColunms);
    table.setPaginationType("client");
    Image.table = table.init();
});
